package com.excilys.training.ebanking.rest.impl;

import static com.google.common.collect.Lists.transform;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.excilys.training.ebanking.iservice.IServiceCompte;
import org.excilys.training.ebanking.iservice.IServiceOperation;
import org.excilys.training.ebanking.model.Operation;
import org.springframework.beans.factory.annotation.Autowired;

import com.excilys.training.ebanking.rest.api.WebServiceRestApi;
import com.excilys.training.ebanking.rest.convertisseur.OperationConvertisseur;
import com.excilys.training.ebanking.rest.model.InfoOperation;
import com.google.common.base.Function;

@Path("/")
@Produces("application/xml")
public class WebServiceRestImpl implements WebServiceRestApi {

	@Autowired
	private IServiceOperation serviceOperation;

	@Autowired
	private IServiceCompte serviceCompte;

	@Autowired
	private OperationConvertisseur convertisseur;

	@Override
	@GET
	@Path("/getVirementByAccount/{idAccount}")
	public List<InfoOperation> getVirementByAccount(
			@PathParam("idAccount") Integer idAccount) {
		List<Operation> listOperation = serviceOperation
				.getAllVirementByAccount(idAccount);

		return transform(listOperation,
				new Function<Operation, InfoOperation>() {
					@Override
					public InfoOperation apply(Operation input) {
						return convertisseur.modelToDto(input);
					}
				});
	}

	@GET
	@Path("/doVirement/{idAccountSource}/{idAccountCible}/{montant}/{operationType}")
	public void doVirementInterne(
			@PathParam("idAccountSource") int idAccountSource,
			@PathParam("idAccountCible") int idAccountCible,
			@PathParam("montant") double montant,
			@PathParam("operationType") int operationTypee) {
		serviceCompte.transfertExterne(idAccountSource, idAccountCible,
				montant, operationTypee);
	}
}
